package com.fsm;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Date;

/*******************
 *@Describe
 *@author 封神梦
 *@Time 2022/8/3
 *@Company http://com.yiview.com
 ****************/
public class JwtDemo {

    public static void main(String[] args) {
        String token = JWT.create()
                .withExpiresAt(new Date(System.currentTimeMillis()+1000*10))  //设置过期时间
                .withAudience("user1") //设置接受方信息，一般时登录用户
                .sign(Algorithm.HMAC256("111111"));  //使用HMAC算法，111111作为密钥加密
        System.out.println("生成的token:"+token);
        try {
            String userId = JWT.decode(token).getAudience().get(0);
            System.out.println("解码的userID:" + userId);
            // Assertions.assertEquals("user1", userId);
            JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256("111111")).build();
            DecodedJWT verify = jwtVerifier.verify(token);
            System.out.println("verify====>"+verify.toString());
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }
}
